package data.preprocessing;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;

import org.annolab.tt4j.DefaultExecutableResolver;
import org.annolab.tt4j.TokenHandler;
import org.annolab.tt4j.TreeTaggerException;
import org.annolab.tt4j.TreeTaggerWrapper;

public class StringProcessor_TreeTagger implements StringProcessor{

	private TreeTaggerWrapper<String> tt ;
	private HashSet<String> acceptedNLP;
	private ArrayList<String> bufWord, bufTag, bufLemma;
	
	//private String[]
	// "/Users/vguigue/Documents/toolbox/nlp/treetagger/tree-tagger-MacOSX-3.2-intel/bin"
	//"treetagger/models/english.par:iso8859-1"

	public static void main(String args[]){
		StringProcessor sp = new StringProcessor_TreeTagger("/Users/vguigue/Documents/toolbox/nlp/treetagger/tree-tagger-MacOSX-3.2-intel/bin",
				"treetagger/models/english.par:iso8859-1", null);
		
		String str = "hello world !";
		
	//	StringProcessor sp2=new StringProcessor_MarkPunctuation();
		
	//	System.out.println(sp.map(sp2.map(str)));
	}
	
	public StringProcessor_TreeTagger(String path2tt,String path2ttmodel, HashSet<String> acceptedNLP){

		this.acceptedNLP = acceptedNLP;
		//
		bufWord=new ArrayList<String>();
		bufTag = new ArrayList<String>();
		bufLemma = new ArrayList<String>();
		
		tt = new TreeTaggerWrapper<String>();
		DefaultExecutableResolver exec = new DefaultExecutableResolver();
		exec.setAdditionalPaths(Arrays.asList(	 new String[] {path2tt}));
		tt.setExecutableProvider(exec);
		try {
			tt.setModel(path2ttmodel);
			tt.setHandler(new TokenHandler<String>() {
				public void token(String token, String pos, String lemma) {
					//System.out.println(token+"\t"+pos+"\t"+lemma);
					
					
					bufWord.add(token); 
					if(pos.indexOf(':')>0)
						pos = pos.substring(0, pos.indexOf(':'));
					bufTag.add( pos);
					bufLemma.add( lemma);
				}
			});
			//  tt.process(Arrays.asList(new String[] {"This", "is", "a", "test", "."}));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public String map(String str) {
		bufWord=new ArrayList<String>();
		bufTag = new ArrayList<String>();
		bufLemma = new ArrayList<String>();
		try {
			tt.process(Arrays.asList(str.split(" ")));
			
		} catch (IOException e) {
			e.printStackTrace();
		} catch (TreeTaggerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String retour="";
		for(int i=0; i<bufTag.size(); i++){
			if(acceptedNLP == null || acceptedNLP.contains(bufTag.get(i)))
				retour += bufLemma.get(i)+" ";
//			else if(bufTag.get(i).equals("SENT"))
//				retour += "FINPHRASE";
		}
		return retour;
	}


}
